home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / EGAVGA.SWG / 0144_Checksums.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  4KB  |  131 lines

  1. {
  2. >  Checksums are not too secure. CRCs are better. In case a word
  3. >  checksum is enough for you...
  4.  
  5. > I'm still not for sure on what this program is suppose to do for me.
  6. > What ever you are talking about above? I have no idea. Well
  7. > I hope to hear from you again, and maybe you can explain on what this
  8. > can do for me and how to use it in my program.
  9.  
  10. Ok. I hope the following examples will make things more clear
  11. to you. Here are two programs. The first one (checksum.pas)
  12. reads a word checksum stored in offset 12h of its own EXE
  13. header. Then it calculates the checksum of its own compiled
  14. code. If both values are the same it prints 'Yes!' else it
  15. prints 'No!'. The second program (writecsum.pas) is a
  16. "utility" that calculates the checksum word for the compiled
  17. code of program checksum.exe and writes it to its header at
  18. offset 12h. For your benefit, both programs use only Pascal
  19. code. For obvious reason the checksum calculation skips the
  20. header itself.
  21.  
  22.    1) Compile both programs into exe files.
  23.    2) Run checksum.exe (it will respond 'No!' because its header
  24.       does not contain the correct checksum).
  25.    3) Run writecsum.exe (this will write the correct checksum into
  26.       the header of checksum.exe)
  27.    4) Run checksum.exe (it will respond 'Yes!')
  28.    5) If the code of checksum.exe is messed in a way that
  29.       its checksum is changed it will again respond 'No!'
  30.  
  31. {-------------------------------------------------}
  32. { This program prints 'Yes!' only if the checksum }
  33. { word stored in offset 12h of the EXE header is  }
  34. { the same as the one calculated by the program   }
  35. { on its own compiled code past the EXE header... }
  36. { -Jose- (1:163/513.3)                            }
  37. {-------------------------------------------------}
  38. program checksum;
  39. uses dos;
  40. type
  41.   arr256 = array[0..255] of byte;
  42. var
  43.   f : file;
  44.   numread : word;
  45.   block   : arr256;
  46.   csum    : word;
  47.   headercsum : word;
  48. function cmem(var block:arr256; siz:word): word;
  49. var
  50.   r : registers;
  51. begin
  52.   r.ax:= $121C;
  53.   r.ds:= seg(block);
  54.   r.si:= ofs(block);
  55.   r.cx:= siz;
  56.   r.dx:= 0;
  57.   Intr($2F,r);
  58.   cmem:= r.dx;
  59. end;
  60. begin
  61.   if lo(dosversion) < 3 then begin
  62.     writeln('Dos must be 3+...');
  63.     halt;
  64.   end;
  65.   assign(f,paramstr(0));
  66.   reset(f,1);
  67.   seek(f,$12);  {seek checksum word in exe header}
  68.   blockread(f,headercsum,2);
  69.   seek(f,$1B);  {skip exe header...}
  70.   csum:= 0;
  71.   repeat        {calculate checksum word}
  72.     blockread(f,block,sizeof(block),numread);
  73.     inc(csum,cmem(block,numread));
  74.   until eof(f) or (numread < sizeof(block));
  75.   if headercsum = csum then
  76.     writeln('Yes! (',csum,')')
  77.   else begin
  78.     writeln('No!');
  79.     writeln(headercsum);
  80.     writeln(csum);
  81.   end;
  82. end.
  83.  
  84. {-----------------------------------------------}
  85. { This program writes the checksum word of      }
  86. { checksum.exe in offset $12 of the EXE header  }
  87. { of the same program -Jose- (1:163/513.3)      }
  88. {-----------------------------------------------}
  89. program writcsum;
  90. uses DOS;
  91. type
  92.   arr256 = array[0..255] of byte;
  93. var
  94.   f : file;
  95.   csum    : word;
  96.   block   : arr256;
  97.   numread : word;
  98. function cmem(var block:arr256; siz:word): word;
  99. var
  100.   r : registers;
  101. begin
  102.   r.ax:= $121C;
  103.   r.ds:= seg(block);
  104.   r.si:= ofs(block);
  105.   r.cx:= siz;
  106.   r.dx:= 0;
  107.   Intr($2F,r);
  108.   cmem:= r.dx;
  109. end;
  110. begin
  111.   if lo(dosversion) < 3 then begin
  112.     writeln('Dos must be 3+...');
  113.     halt;
  114.   end;
  115.   assign(f,'checksum.exe');
  116.   {$I-} reset(f,1); {$I+}
  117.   if ioresult <> 0 then begin
  118.     writeln('Cannot find file checksum.exe');
  119.     halt;
  120.   end;
  121.   seek(f,$1B); {skip header...}
  122.   csum:= 0;
  123.   repeat       {calculate checksum word}
  124.     blockread(f,block,sizeof(block),numread);
  125.     inc(csum,cmem(block,numread));
  126.   until eof(f) or (numread < sizeof(block));
  127.   seek(f,$12); {seek checksum word in exe header}
  128.   blockwrite(f,csum,sizeof(csum));
  129.   close(f);
  130. end.
  131.